package com.neolix.watch;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.neolix.watch.bluetooth.BlueToothUtil;
import com.neolix.watch.bluetooth.BluetoothListActivity;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import static android.support.v4.view.ViewPager.OnPageChangeListener;
import static android.view.KeyEvent.KEYCODE_BACK;
import static android.view.View.OnClickListener;
import static com.neolix.watch.NeolixApplication.getInstance;
import static com.neolix.watch.util.Constant.ONE_SECOND;
import static com.neolix.watch.util.Constant.PAGE_COUNT;

public class GuideActivity extends NeolixBaseActivity implements OnClickListener, OnPageChangeListener {
    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;
    private ArrayList<View> views;

    private ImageView[] points;
    private int currentPageIndex;
    private Button loginButton;
    private boolean isExit;
    private int REQUEST_ENABLE_BT = 1;
    private int REQUEST_SCAN_BLUETOOTH = 2;

    public GuideActivity() {
        super(R.layout.activity_guide);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
        initData();
        initListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public boolean onKeyDown(int keyCode, @NotNull KeyEvent event) {
        if (keyCode != KEYCODE_BACK) {
            return super.onKeyDown(keyCode, event);
        } else {
            exit();
            return true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_OK) {
            findDevice();
        } else {
            goTo(MainActivity.class);
            finish();
        }
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        loginButton = (Button) findViewById(R.id.register);
        views = new ArrayList<View>();
        viewPagerAdapter = new ViewPagerAdapter(views);
        loginButton.setVisibility(View.GONE);
    }

    private void initData() {
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        views.add(inflater.inflate(R.layout.view_guide_one, null));
        views.add(inflater.inflate(R.layout.view_guide_two, null));
        views.add(inflater.inflate(R.layout.view_guide_three, null));

        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setOnPageChangeListener(this);

        initPoint();
    }

    private void initPoint() {
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.points);
        points = new ImageView[PAGE_COUNT];

        for (int i = 0; i < PAGE_COUNT; i++) {
            points[i] = (ImageView) linearLayout.getChildAt(i);
            points[i].setEnabled(true);
            points[i].setOnClickListener(this);
            points[i].setTag(i);
        }

        currentPageIndex = 0;
        points[currentPageIndex].setEnabled(false);
    }

    private void initListener() {
        loginButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                getInstance().initApp();
                connectBluetooth();
            }
        });
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int position) {
        setCurrentDot(position);
    }

    @Override
    public void onClick(View point) {
        int position = (Integer) point.getTag();
        setCurrentView(position);
        setCurrentDot(position);
    }

    private void setCurrentView(int position) {
        if (position < 0 || position >= PAGE_COUNT) {
            return;
        }
        viewPager.setCurrentItem(position);
    }

    private void setCurrentDot(int position) {
        if (position < 0 || position > PAGE_COUNT - 1 || currentPageIndex == position) {
            return;
        }
        points[position].setEnabled(false);
        points[currentPageIndex].setEnabled(true);

        currentPageIndex = position;
        if (currentPageIndex == PAGE_COUNT - 1)
            loginButton.setVisibility(View.VISIBLE);
        else
            loginButton.setVisibility(View.GONE);
    }

    private void exit() {
        if (isExit) {
            finish();
        } else {
            isExit = true;
            Toast.makeText(getApplicationContext(), getString(R.string.tip_exit), Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(0, 3 * ONE_SECOND);
        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    private void connectBluetooth() {
        if (!BlueToothUtil.isEnable()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        } else {
            findDevice();
        }
    }

    private void findDevice() {
        Intent enableBtIntent = new Intent(getApplicationContext(), BluetoothListActivity.class);
        startActivityForResult(enableBtIntent, REQUEST_SCAN_BLUETOOTH);
    }
}
